﻿using System;
using System.Threading;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEDHelper;
using System.Runtime.InteropServices;

namespace LEDManager
{

    public partial class LEDConsole
    {
        Int32 ConvertEffect(int nEffectType)
        {
            /*
      method: 显示方式
       * 1.   立即显示 2.   左滚显示    3.   连续上滚   4.   中间向上下展开   5.   中间向两边展开   6.   中间向四周展开   7.   向左移入
       * 8.   向右移入   9.   从左向右展开  10.   从右向左展开  11.   右上角移入  12.   右下角移入  13.   左上角移入  14.   左下角移入
       * 15.   从上向下移入  16.   从下向上移入  17.   闪  烁
       * speed:   显示速度（1-8）越大越快
       * transparent: 是否透明   0=不透明 1=透明
             *                 //上滚文字=4
              LED_BeginProgram(nLEDID, 2000, szBackBMP);
                AddTextBlock_Click("上滚文字,x=10,y=2,宽=128,高=32,速度=1", 10, 2, 128, 32, 4, 1);
                //下滚文字=16
                LED_BeginProgram(nLEDID, 2000, szBackBMP);
                AddTextBlock_Click("下滚文字", 10, 2, 128, 32, 16, 1); 
             
      */
            int method = 1;
            switch (nEffectType)
            {
             case 0:
                    method = 1;
                    break;
                case 1:
                    method = 2;
                    break;
                case 2:
                    method = 9;
                    break;
                case 3:
                    method = 16;
                    break;
                case 4:
                    method = 4;
                    break;
                case 5:
                    method = 5;
                    break; 
                default:
                    //method = nEffectType;
                    break;
            }
            return method;
        }
        Int32 ConvertSpeed(int nSpeed)
        {
            int speed = 1;
            switch (nSpeed)
            {
                case 1:
                default:
                    speed = nSpeed;
                    break;
            }
            if (speed > 7)
                speed = 7;
            return speed;
        }
        Int32 ConvertBright(int iValue)
        {
            int bright = 7;
            switch (iValue)
            {
                case 1:
                default:
                    bright = iValue;
                    break;
            }
            if (bright > 7)
                bright = 7;
            return bright;
        }
        Int32 ConvertTrasnparent(int lTrasnparent)
        {
            int transparent = 0;
            switch (lTrasnparent)
            {
                case 1:
                default:
                    transparent = lTrasnparent;
                    break;
            }
            return transparent;
        }
        int sleep = 200;
        //外部向中心请求，中心定位到对应的显示屏，向该显示屏请求。
        LEDUnit GetLED(int nLEDID)
        {
            LEDUnit led = null;
            try
            {
                foreach (KeyValuePair<string, LEDUnit> kp in ledList)
                {
                    LEDUnit ledtemp = (LEDUnit)kp.Value;
                    if (ledtemp.ledid == nLEDID)
                        led = ledtemp;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return led;
        }
        public Int32 LED_IsDeviceOK(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_Query");
                Thread.Sleep(sleep);

                result = System.Convert.ToUInt16(led.dwLEDStatus["STSTATUS"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_SwitchPower(int nLEDID, int nTurnOn)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_SetPower", nTurnOn);
                result = System.Convert.ToInt32(led.dwLEDStatus["STPOWER"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_IsDeviceTurnOn(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_GetPower");
                result = System.Convert.ToUInt16(led.dwLEDStatus["STPOWER"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_SetBright(int nLEDID, Int32 lValue)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                int bright = ConvertBright(lValue);
                result = (Int32)led.PerformCommand("LED_SetBrightness", bright);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                result = 0;
            }
            return result;
        }
        public Int32 LED_GetBright(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_GetBrightness");
                Thread.Sleep(sleep);
                result = System.Convert.ToUInt16(led.dwLEDStatus["STBRIGHT"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_Restart(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_SetPower", 0);
                result = (Int32)led.PerformCommand("LED_SetPower", 1);
                result = System.Convert.ToInt32(led.dwLEDStatus["STPOWER"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_AdjustCurrentTime(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_AdjustTime");
                Thread.Sleep(sleep);
                result = System.Convert.ToUInt16(led.dwLEDStatus["STSETCLOCK"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }

        public int LED_PreUploadBMP(int nLEDID, string szBmpFile)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_PreUploadBMP", szBmpFile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public int LED_RemoveUploadedBmp(int nLEDID, string szBmpFile)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_RemoveUploadedBmp", szBmpFile);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public int LED_GetFreeUploadBmpSpace(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                result = (Int32)led.PerformCommand("LED_GetFreeUploadBmpSpace");
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = System.Convert.ToUInt16(led.dwLEDStatus["STFREESPACE"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_LEDScreenInit(int nLEDID, int nScreenType, int nWidth, int nHeight)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                nScreenType = LEDAPI.DEVICE_TYPE_UDP;
                result = (Int32)led.PerformCommand("LED_Open", nScreenType, nWidth, nHeight, LEDAPI.ROOT_PLAY);
                Thread.Sleep(sleep);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_BeginProgram(int nLEDID, Int32 lDisplayTime, string szBackBMP)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("AddLeaf", lDisplayTime, szBackBMP);
                Thread.Sleep(sleep);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }

        public void LED_AddTextBlock(int nLEDID, string szText, LEDBlock rect, ref FontDescriptor lpFont, int nEffectType, int nSpeed, Int32 lTrasnparent)
        {
            Int32 result = 0;
            try
            {
                LEDUnit led = null;
                led = GetLED(nLEDID);
                if (led == null)
                    return;

                int method = ConvertEffect(nEffectType);
                int speed = ConvertSpeed(nSpeed);
                int transparent = ConvertTrasnparent(lTrasnparent);
                result = (Int32)led.PerformCommand("AddText", szText, rect, lpFont, method, speed, transparent);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LED_AddBmpBlock(int nLEDID, string szBmpFile, LEDBlock destRect, LEDBlock sourceRect, int nEffectType, int nSpeed, Int32 lTrasnparent)
        {
            Int32 result = 0;
            try
            {
                LEDUnit led = null;
                led = GetLED(nLEDID);
                if (led == null)
                    return;
                int method = ConvertEffect(nEffectType);
                int speed = ConvertSpeed(nSpeed);
                int transparent = ConvertTrasnparent(lTrasnparent); ;
                result = (Int32)led.PerformCommand("AddWindow", szBmpFile, destRect, sourceRect, method, speed, transparent);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LED_AddClockBlock(int nLEDID, LEDBlock rect, ref ClockDescriptor lpClock, ref FontDescriptor lpFont, int nEffectType, int nSpeed, Int32 lTrasnparent)
        {
            Int32 result = 0;
            try
            {
                LEDUnit led = null;
                led = GetLED(nLEDID);
                if (led == null)
                    return;
                int method = ConvertEffect(nEffectType);
                int speed = ConvertSpeed(nSpeed);
                int transparent = ConvertTrasnparent(lTrasnparent);
                result = (Int32)led.PerformCommand("AddDateTime", rect, lpClock, lpFont, method, speed, transparent);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public Int32 LED_SendToScreen(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_SendToScreen");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public Int32 LED_EndProgram(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_EndProgram");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }

        public Int32 LED_FastenProgram(int nLEDID)
        {
            Int32 result = 0;
            LEDUnit led = null;
            try
            {
                led = GetLED(nLEDID);
                if (led == null)
                    return result;
                result = (Int32)led.PerformCommand("LED_FastenProgram");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }

    }
}
